package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Icon;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.layout.RegionActions;
import xworker.javafx.util.JavaFXUtils;

public class IconActions {
    static{
        PropertyFactory.regist(Icon.class, "contentProperty", o -> {
            Icon obj = (Icon) o;
            return obj.contentProperty();
        });
    }

    public static void init(Icon node, Thing thing, ActionContext actionContext) {
        RegionActions.init(node, thing, actionContext);

        MaterialDesignIcon content = JavaFXUtils.getObject(thing, "content", actionContext);
        if(content == null){
            Thing contentThing = thing.getThing("Content@0");
            if(contentThing != null){
                content = contentThing.doAction("create", actionContext);
            }
        }
        if (content != null) {
            node.setContent(content);
        }

    }

    public static Icon create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        Icon node = new Icon();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }
}